package com.crm.system.controller.cluePoolManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.contactManager.Contact;
import com.crm.model.entity.customerManager.CustomerContactPerson;
import com.crm.model.entity.customerManager.SingleProductCustomerContact;
import com.crm.model.qo.cluePoolManager.ClueContactQO;
import com.crm.model.vo.cluePoolManager.ClueContactVO;
import com.crm.system.service.cluePoolManager.ClueContactService;
import com.crm.system.service.contactManager.ContactService;
import com.crm.system.service.customerManager.CustomerContactPersonService;
import com.crm.system.service.customerManager.SingleProductCustomerContactService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wll
 * @date 2021/1/25 15:30
 * info 线索联系人
 */
@RestController
@RequestMapping("/clueContact")
@Slf4j
public class ClueContactController {

    @Autowired
    private ClueContactService clueContactService;

    @Autowired
    private CustomerContactPersonService customerContactPersonService;

    @Autowired
    private SingleProductCustomerContactService singleProductCustomerContactService;

    @Autowired
    private ContactService contactService;

    /**
     * @author wll
     * @date 2021/1/25 15:30
     * info 用户模块-我的线索池-线索清单-新增线索联系人
     * @param clueContactQO
     * @return
     */
    @PostMapping("/save/clueContact")
    public DataResponse add(@RequestBody ClueContactQO clueContactQO) {
        try {
            Boolean result = clueContactService.addClueContact(clueContactQO);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            } else {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
        }catch (Exception e){
            log.error("clueContact saveClueContact error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }


    /**
     * @author wll
     * @date 2021/1/26 16:32
     * info 用户模块-我的线索池-线索清单-跟进-线索联系人回显 / 用户模块-客户管理-我的客户-跟进-客户联系人回显 / 用户模块-单品客户-跟进-联系人回显
     * @param status 0 查询全部联系人和手机号 1 模糊搜索联系人和手机号
     * @param id 线索/客户id
     * @param name  status为1的时候 模糊搜索
     * @param source 从哪跳转到跟进页面（0：线索；1：客户）
     * @return
     */
    @PostMapping("/clueContact/echo")
    public DataResponse clueContactEcho(@RequestParam(value="status")String status,
                                        @RequestParam(value="id")String id,
                                        @RequestParam(value="name",required = false)String name,
                                        @RequestParam(value="source")String source) {
        try {
            if ("0".equals(source)) {
                List<ClueContactVO> clueContactVOs = clueContactService.clueContactEcho(status,id,name);
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),clueContactVOs);
            } else if ("1".equals(source)) {
                //List<CustomerContactPerson> customerContactPeoples = customerContactPersonService.selectCustomerContactPersons(status, id, name);
                List<Contact> customerContactPeoples = contactService.contactByCustomerIdList(id);
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerContactPeoples);
            }  else if ("2".equals(source)) {
                List<SingleProductCustomerContact> singleProductCustomerContacts = singleProductCustomerContactService.selectSingleProductCustomerContacts(status, id, name);
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),singleProductCustomerContacts);
            }
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }catch (Exception e){
            log.error("clueContact echo error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }









































}
